package chen.bupt.mapreduce.feature;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.ansj.util.recognition.NatureRecognition;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import chen.bupt.constant.Constants;
import chen.bupt.text.StopWordUtil;
import chen.bupt.text.TextClear;
import chen.bupt.util.HDFSFileUtils;

public class FeatureSortJob {
	private static final String inputPath = Constants.FEATURE_TERM_PATH;
	private static final String outputPath = Constants.FEATURE_TERM_SORTED_PATH;

	public static class Map1 extends Mapper<LongWritable, Text, Text, Text> {
		private static final Text k = new Text("one");

		public void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			if (!HDFSFileUtils.isInputFile(context))
				return;
			if (value.toString().startsWith("featureNum:"))
				return;
			context.write(k, value);
		}
	}

	public static class Reduce1 extends Reducer<Text, Text, Text, NullWritable> {
		protected void reduce(Text key, Iterable<Text> values, Context context)
				throws IOException, InterruptedException {
			Set<String> set = new TreeSet<String>();
			for (Text t : values) {
				String strs[] = t.toString().split(";");
				for (String s : strs) {
					List<Term> terms = ToAnalysis.paser(s);
					new NatureRecognition(terms).recognition();
					String term = null;
					for (Term tm : terms) {
						if ((term = TextClear.getValidityTerm(tm.toString())) != null) {
							if (!(StopWordUtil.isStopWord(term) || StopWordUtil
									.isSpecialTag(term))) {
								set.add(term);
							}
						}
					}
				}
			}

			StringBuffer sb = new StringBuffer();
			Iterator<String> ite = set.iterator();
			while (ite.hasNext()) {
				sb.append(ite.next() + ";");
			}
			context.write(new Text(sb.toString()), NullWritable.get());
			context.write(new Text("featureNum:" + set.size()), NullWritable
					.get());
		}
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = new Job(conf, "featureExtract");
		job.setJarByClass(FeatureSortJob.class);
		job.setMapperClass(Map1.class);
		job.setReducerClass(Reduce1.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		job.setNumReduceTasks(1);
		Path input = new Path(inputPath);
		Path output = new Path(outputPath);
		HDFSFileUtils.deleteFile(output, conf);
		FileInputFormat.addInputPath(job, input);
		FileOutputFormat.setOutputPath(job, output);
		System.exit(job.waitForCompletion(true) ? 0 : 1);

	}

}
